package cn.lyjuan.aliyun.mq.conf.java.dynamic;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.util.StringUtils;

/**
 * 组合模式下的抽象类
 * Created by chad on 2016/10/10.
 */
public abstract class ADynamicBean
{
    private static Logger log = LogManager.getLogger(ADynamicBean.class.getName());

    /**
     * SpringBean基本名称（即xml id属性），便于统一管理
     */
    protected String baseName;

    /**
     * 名称后缀
     */
    protected String nameSuffix;

    /**
     * 激活策略
     */
    protected String profile;

    /**
     * 循环注册所有Bean
     *
     * @return
     */
    public abstract void register(DefaultListableBeanFactory beanFactory);

    /**
     * Spring中是否已注册这个Bean
     * @param beanFactory
     * @return
     */
    protected boolean isContainsBean(DefaultListableBeanFactory beanFactory)
    {
        if (StringUtils.isEmpty(this.getBaseName())) return false;

        return beanFactory.containsBean(this.getQuiteBeanName());
    }

    public ADynamicBean(String baseName, String nameSuffix, String profile)
    {
        this.baseName = baseName;
        this.nameSuffix = nameSuffix;
        this.profile = profile;
    }

    public ADynamicBean(String baseName, String nameSuffix)
    {
        this.baseName = baseName;
        this.nameSuffix = nameSuffix;
    }

    public ADynamicBean()
    {
    }

    /**
     * 返回该Bean在Spring ApplicationContext中的全名
     * @return
     */
    public String getQuiteBeanName(String nameSuffix)
    {
        if (StringUtils.isEmpty(this.getBaseName()))
            throw new RuntimeException("baseName can not Empty");

        return this.getBaseName() + nameSuffix;
    }
    public String getQuiteBeanName() {return getBaseName() + getNameSuffix();}
    public String getBaseName() {return baseName;}
    public void setBaseName(String baseName) {this.baseName = baseName;}
    public String getProfile() {return profile;}
    public void setProfile(String profile) {this.profile = profile;}
    public String getNameSuffix() {return nameSuffix;}
    public void setNameSuffix(String nameSuffix) {this.nameSuffix = nameSuffix;}
}
